package edu.faytechcc.csc251.gui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author kent
 */
public class ResultsPanel
        extends JPanel {

    private final JTable table;
    private final JScrollPane scrollDisplay;
    private final Model model;
    private final String query;

    /**
     * Constructor
     *
     * @param query
     */
    public ResultsPanel(String query) {
        super(new BorderLayout());
        this.setBorder(BorderFactory.createTitledBorder("Results"));
        this.query = query;
        model = new Model(this.query);
        table = new JTable(model);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setAutoCreateRowSorter(true);

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        scrollDisplay = new JScrollPane(table);
        scrollDisplay.setHorizontalScrollBarPolicy(
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        this.add(scrollDisplay,
                BorderLayout.CENTER);
    }

//    public void setModelData(String[][] data) {
//        this.model.setData(data);
//    }
    
    public void setModelList(ArrayList dataList)
    {
        this.model.setDataList(dataList);
    }
    
    public ArrayList getModelList()
    {
        return this.model.getDataArrayList();
    }
    
    public String[] getModelColumns()
    {
        return this.model.getColNames();
    }
    
//    public String[][] getModelData()
//    {
//        return this.model.getData();
//    }
    public String getSelectedCardName() {
        int row = this.table.getSelectedRow();

        String cardName = (String) model.getValueAt(row, 0);
        return cardName;
    }

    public String getSelectedCardSet() {
        int row = this.table.getSelectedRow();

        String setName = (String) model.getValueAt(row, 1);
        return setName;
    }

    public String[] getSelectedCardInfo() {
        int row = this.table.getSelectedRow();

        String[] cardInfo = new String[11];
        for (int i = 0; i < 11; i++) {
            cardInfo[i] = (String) model.getValueAt(row, i);
        }
        return cardInfo;
    }

    public int getSelectedCardCollectionNum() {
        int row = this.table.getSelectedRow();
        String value = (String)this.model.getValueAt(row, 11);
        value = value.trim();
        int collectionNum = Integer.parseInt(value);
        return collectionNum;
    }
    
    public JTable getTable()
    {
        return table;
    }
    
    public void addImageListenerToTable(ListSelectionListener imageListener)
     {
         table.getSelectionModel().addListSelectionListener(imageListener);
     }
}
